package com.adingxiong.cft.controller;


import com.adingxiong.cft.annotate.StrCut;
import com.adingxiong.cft.common.FileData;
import com.adingxiong.cft.design.Mulitipart;
import com.adingxiong.cft.entity.SysLog;
import com.adingxiong.cft.errors.ApiException;
import com.adingxiong.cft.result.Result;
import com.adingxiong.cft.service.ISysLogService;
import com.adingxiong.cft.util.ExcelTools;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.poiutils.code.ExcelExport;
import com.gitee.poiutils.code.ExcelImport;
import com.gitee.poiutils.code.ExcelToHtml;
import com.gitee.poiutils.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-09-27
 */
@RestController
@RequestMapping("/syslog")
@Api(tags = "系统日志相关")
public class SysLogController {

    @Autowired
    private ISysLogService logService;


    @GetMapping("/list")
    @ApiOperation(value = "获取操作日志")
    public Result<Page<SysLog>> getList(
            @ApiParam(value = "keywords" ,name = "关键字") @RequestParam(value = "keywords" ,required = false) String keywords,
            @ApiParam(value = "pageSize",name = "当前页码") @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
            @ApiParam(value = "pageNum",name = "每页条数") @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum
    ){
        Page<SysLog> userPage = logService.list(keywords,pageSize,pageNum);
        return Result.success(userPage);
    }

    @ApiOperation(value = "导出操作日志")
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response, HttpServletRequest request){
        List<SysLog> list = logService.list();

        HSSFWorkbook workbook = new HSSFWorkbook();
        ExcelExport.getInstance().fillCommon(workbook,list,"日志导出");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        FileData fileData = new FileData();
        try {
            workbook.write(os);
            os.flush();
            fileData.setBytes(os.toByteArray());
            fileData.setFileName("操作日志导出.xls");
            ExcelTools.setResponse(request,response,fileData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "在线预览文件")
    @GetMapping("/viewExcel")
    public String viewExcel(){
        List<SysLog> list = logService.list();

        HSSFWorkbook workbook = new HSSFWorkbook();
        ExcelExport.getInstance().fillCommon(workbook,list,"日志导出");
        String s = ExcelToHtml.getInstance().excelToH5(null, workbook, true, 1);
        return s;
    }

    @PostMapping(value = "/dataImport")
    @ApiOperation(value = "导入数据")
    public Result<List<SysLog>> importData(
         @RequestBody  MultipartFile file
    ){
        if(file == null ){
            throw new ApiException("获取文件失败");
        }
        try {
            Workbook workbook = ExcelTools.readExcel(file);
            List<SysLog> transformation = ExcelImport.getInstance()
                    .isFormatTitle(true)
                    .setFieldError("error")
                    .setFieldError("rows")
                    .transformation(workbook.getSheet("日志导出"), SysLog.class);

            if(!CollectionUtils.isEmpty(transformation)) {
                List<SysLog> collect = transformation.stream().filter(e -> StringUtils.isEmpty(e.getError())).collect(Collectors.toList());
                boolean b = logService.saveBatch(collect);
                System.out.println(b);
            }
            return Result.success(transformation);



        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}

